package attendance

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/attendance"
	attendanceReq "github.com/flipped-aurora/gin-vue-admin/server/model/attendance/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ClassApi struct {
}

var classService = service.ServiceGroupApp.AttendanceServiceGroup.ClassService

// CreateClass 创建班级
// @Tags Class
// @Summary 创建班级
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.Class true "创建班级"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /class/createClass [post]
func (classApi *ClassApi) CreateClass(c *gin.Context) {
	var class attendance.Class
	err := c.ShouldBindJSON(&class)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	class.BelongTo = utils.GetBelongTo(c)
	if err := classService.CreateClass(&class); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteClass 删除班级
// @Tags Class
// @Summary 删除班级
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.Class true "删除班级"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /class/deleteClass [delete]
func (classApi *ClassApi) DeleteClass(c *gin.Context) {
	ID := c.Query("ID")
	if err := classService.DeleteClass(ID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteClassByIds 批量删除班级
// @Tags Class
// @Summary 批量删除班级
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /class/deleteClassByIds [delete]
func (classApi *ClassApi) DeleteClassByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := classService.DeleteClassByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateClass 更新班级
// @Tags Class
// @Summary 更新班级
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body attendance.Class true "更新班级"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /class/updateClass [put]
func (classApi *ClassApi) UpdateClass(c *gin.Context) {
	var class attendance.Class
	err := c.ShouldBindJSON(&class)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := classService.UpdateClass(class); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindClass 用id查询班级
// @Tags Class
// @Summary 用id查询班级
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query attendance.Class true "用id查询班级"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /class/findClass [get]
func (classApi *ClassApi) FindClass(c *gin.Context) {
	ID := c.Query("ID")
	if reclass, err := classService.GetClass(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reclass": reclass}, c)
	}
}

// GetClassList 分页获取班级列表
// @Tags Class
// @Summary 分页获取班级列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query attendanceReq.ClassSearch true "分页获取班级列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /class/getClassList [get]
func (classApi *ClassApi) GetClassList(c *gin.Context) {
	var pageInfo attendanceReq.ClassSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if list, total, err := classService.GetClassInfoList(pageInfo, utils.GetBelongTo(c)); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetClassPublic 不需要鉴权的班级接口
// @Tags Class
// @Summary 不需要鉴权的班级接口
// @accept application/json
// @Produce application/json
// @Param data query attendanceReq.ClassSearch true "分页获取班级列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /class/getClassPublic [get]
func (classApi *ClassApi) GetClassPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的班级接口信息",
	}, "获取成功", c)
}
